package com.ghca.myinfo.system.controllers;

import com.ghca.myinfo.system.entities.Permission;
import com.ghca.myinfo.system.entities.User;
import com.ghca.myinfo.system.services.PermissionService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * Created by gh on 2017/1/25.
 */
@Controller
public class PermissionController {

    @Autowired
    private PermissionService permissionService;

    @ResponseBody
    @RequestMapping("/permission/list/{parentId}")
    public List<Permission> list(@PathVariable(required = false, name = "parentId") String parentId, @ModelAttribute(name = "currentUser") User currentUser) {
        if(StringUtils.isBlank(parentId)|| StringUtils.equals("0", parentId)) {
            return  permissionService.findRootPermission(currentUser.getRoles());
        }else {
            return permissionService.findByParent(currentUser.getRoles(), parentId);
        }
    }
}
